package modbusGreen

import (
    "fmt"
    "github.com/astaxie/beego/orm"
)

// RequestFrame 请求帧
type RequestFrame struct {
    Id              int     `json:"id",orm:"auto"`
    NodeCode        uint    `json:"node_code"`
    FuncCode        uint    `json:"func_code"`
    Addr            uint    `json:"addr"`
    DataLen01       uint    `json:"data_len_01"`
    DataLen02       uint    `json:"data_len_02"`
}

// ResponseFrame 响应帧
type ResponseFrame struct {
    Id              int     `json:"id",orm:"auto"`
    NodeCode        uint    `json:"node_code"`
    FuncCode        uint    `json:"func_code"`
    DataCount       uint    `json:"data_count"`
    Datas           []uint    `json:"datas"`
}

// SensorType 传感器类型
type SensorType struct {
    Id              int     `json:"id",orm:"auto"`
    Name            string  `json:"name"`
    InstrumentMultiple int  `json:"instrument_multiple" sql:"instrument_multiple"`
}

// Sensor 传感器结构
type Sensor struct {
    Id              int     `json:"id",orm:"auto"`
    SensorTypeId    int     `json:"sensor_type_id" sql:"sensor_type_id" orm:"column(sensor_type_id)"`
    FarmId          int     `json:"farm_id" sql:"farm_id" `
    Name            string  `json:"name"`
    NodeId          int     `json:"node_id" sql:"node_id"`
}

// Register 寄存器结构
type Register struct {
    Id              int     `json:"id",orm:"auto"`
    SensorTypeId    int     `json:"sensor_type_id" sql:"sensor_type_id" orm:"column(sensor_type_id)"`
    RegisterAddr    int     `json:"register_addr" sql:"register_addr"`
    RegisterName    string  `json:"register_name" sql:"register_name"`
    RegisterLength  int     `json:"register_length" sql:"register_length"`
    IntervalTime    int     `json:"interval_time" sql:"interval_time"`
    Uint            string  `json:"uint"`
    Max             int     `json:"max"`
    Min             int     `json:"min"`
}

// RegisterRecord 寄存器记录数据结构
type RegisterRecord struct {
    Id              int     `json:"id",orm:"auto"`
    RegisterId      int     `json:"register_id" sql:"register_id"`
    SensorId        int     `json:"sensor_id" sql:"sensor_id"`
    Value           int     `json:"value"`
    Time            int64   `json:"time"`
    Unit            string  `json:"unit"`
}

// AddSensor 添加传感器(通过轻量版orm)
func AddSensor1(sensor *Sensor) (err error) {
    err = ORM.Insert("sensor", sensor)
    if err != nil {
        return
    }
    return nil
}

// AddSensor 添加传感器
func AddSensor(sensor *Sensor) (err error) {
    O := &DB{Ormer: orm.NewOrm()}
    O.Begin()
    _, err = O.Insert(sensor)
    if err != nil {
        O.Rollback()
        return
    } else {
        O.Commit()
        return nil
    }

}

// UpdateSensorName 通过传感器ID，更新传感器名
func UpdateSensorName(sensorId int, name string) (num int64, err error) {
    O := &DB{Ormer: orm.NewOrm()}
    sensor := Sensor{Id:sensorId}
    err = O.Read(&sensor)
    sensor.Name = name
    if err != nil {
        fmt.Println(err)
    }
    num, err = O.Update(&sensor)
    return
}

// GetSensorById 通过ID获取指定传感器
func GetSensorById(id int) (sensor Sensor, err error) {
    O := &DB{Ormer: orm.NewOrm()}
    err = O.SELECT("sensor","WHERE id=?", &sensor, id)
    if err != nil {
        return
    }
    return
}

// DelSensor 通过ID删除指定传感器
func DelSensor(id int) (err error) {
    O := &DB{Ormer: orm.NewOrm()}
    if _, err := O.Delete(&Sensor{Id:id}); err != nil {
        return err
    } else {
       return nil
    }
}



// AddSensorType 添加传感器类型
func AddSensorType(sensorType *SensorType) (err error) {
    O := &DB{Ormer: orm.NewOrm()}
    O.Begin()
    _, err = O.Insert(sensorType)
    if err != nil {
        O.Rollback()
        return
    } else {
        O.Commit()
        return nil
    }
}

// GetSensorTypeById 通过ID，获取指定传感器类型
func GetSensorTypeById(id int) (sensorType SensorType, err error) {
    O := &DB{Ormer: orm.NewOrm()}
    err = O.SELECT("sensor_type","WHERE id=?", &sensorType, id)
    if err != nil {
        return
    }
    return
}

// DelSensorType 删除传感器类型
func DelSensorType(id int) (err error) {
    O := &DB{Ormer: orm.NewOrm()}
    if _, err := O.Delete(&SensorType{Id:id}); err != nil {
        return err
    } else {
        return nil
    }
}



// AddRegister 添加寄存器
func AddRegister(register *Register) (err error) {
    O := &DB{Ormer: orm.NewOrm()}
    O.Begin()
    _, err = O.Insert(register)
    if err != nil {
        O.Rollback()
        return
    } else {
        O.Commit()
        return nil
    }
}

// GetRegisterById 通过寄存器ID， 获取指定寄存器
func GetRegisterById(id int) (register Register, err error) {
    O := &DB{Ormer: orm.NewOrm()}
    err = O.SELECT("register","WHERE id=?", &register, id)
    if err != nil {
        return
    }
    return
}

// DelRegister 删除寄存器
func DelRegister(id int) (err error) {
    O := &DB{Ormer: orm.NewOrm()}
    if _, err := O.Delete(&Register{Id:id}); err != nil {
        return err
    } else {
        return nil
    }

}

func GetRegisterBySensor(id int) (registers []Register, err error) {
    O := &DB{Ormer: orm.NewOrm()}
    err = O.SELECTS("register", "WHERE sensor_type_id=?", &registers, id)
    if err != nil {
        return
    }
    return
}



// AddRegisterRecord 添加记录
func AddRegisterRecord(registerRecord *RegisterRecord) (err error) {
    O := &DB{Ormer: orm.NewOrm()}
    O.Begin()
    _, err = O.Insert(registerRecord)
    if err != nil {
        O.Rollback()
        return
    } else {
        O.Commit()
        return nil
    }
}

// GetRegisterRecordById 通过ID， 获取指定记录
func GetRegisterRecordById(id int) (registerRecord RegisterRecord, err error) {
    O := &DB{Ormer: orm.NewOrm()}
    err = O.SELECT("register_record","WHERE id=?", &registerRecord, id)
    if err != nil {
        return
    }
    return
}

// DelRegisterRecord 删除记录
func DelRegisterRecord(id int) (err error) {
    O := &DB{Ormer: orm.NewOrm()}
    if _, err := O.Delete(&RegisterRecord{Id:id}); err != nil {
        return err
    } else {
        return nil
    }

}